/**
 * Created with hong.rong
 * Date:2020/07/06
 */

import React from 'react'
import { Table } from 'antd'
import Search from './components/Search'
import useOrder from './useOrder'

export default function Index() {
  const {
    loading,
    searchValue,
    total,
    dataSource,
    handleFetchList,
  } = useOrder()

  const renderColumn = () => {
    return [
      {
        title: '序列',
        dataIndex: 'sortNum',
        render: (_, _2, i) => i + 1,
      },
      {
        title: '团购ID',
        dataIndex: 'groupId',
      },
      {
        title: '名称',
        dataIndex: 'title',
      },
      {
        title: '图片',
        dataIndex: 'img',
      },
      {
        title: '订单号',
        dataIndex: 'groupUserNum',
      },
      {
        title: '自提点',
        dataIndex: 'startTime',
      },
      {
        title: '下单时间',
        dataIndex: 'orderTime',
      },
      {
        title: '团购开始时间',
        dataIndex: 'endTime',
      },
      {
        title: '订单状态',
        dataIndex: 'groupStatus',
      },
      {
        title: '备注',
        dataIndex: 'subTitle',
      },
      // {
      //   title: '操作',
      //   dataIndex: 'action',
      //   width: 80,
      //   render: (_, r) => (
      //     <div className='ub ub-row ub-ac'>
      //       <div className="link-button" onClick={() => {}}>
      //         详情
      //       </div>
      //       <div className="link-button ml-10" onClick={() => {}}>
      //         修改
      //       </div>
      //     </div>
      //   ),
      // },
    ]
  }

  /**
   * 搜索
   * @param search
   * @returns {Promise<void>}
   */
  const handleSearch = async (search = {}) => {
    await handleFetchList({ ...searchValue, ...search })
  }
  /**
   * 翻页
   * @param pagination
   * @returns {Promise<void>}
   */
  const handlePageChange = async pagination => {
    await handleFetchList({
      ...searchValue,
      page: pagination.current,
      pageSize: pagination.pageSize,
    })
  }
  return (
    <div>
      <Search
        onSearch={handleSearch}
        refreshList={() => handleFetchList(searchValue)}
      />
      <Table
        loading={loading}
        pagination={{
          current: searchValue.page,
          pageSize: searchValue.pageSize,
          total: total,
        }}
        dataSource={dataSource}
        columns={renderColumn(searchValue)}
        rowKey={(v, i) => i}
        onChange={handlePageChange}
      />
    </div>
  )
}
